package com.wangwei.spring.rabbit;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.TimeUnit;

/**
 * 只有在SimpleMessageListenerContainer生命周期内才能对消息进行消费。
 * Created by wangwei on 2019/10/11 0011.
 */
@ComponentScan
@EnableRabbit
@PropertySource(value = {"classpath:app.properties"})
public class App {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class);
        System.out.println("spring startup");
//        TimeUnit.SECONDS.sleep(20);
//        SimpleMessageListenerContainer simpleMessageListenerContainer = context.getBean(SimpleMessageListenerContainer.class);
        //运行时动态设置并发消费者个数
//        simpleMessageListenerContainer.setConcurrentConsumers(20);
//        System.out.println("setConcurrentConsumers -> 5");
//        simpleMessageListenerContainer.addQueueNames("mail","weixin");
//        System.out.println("add new queue success");
//
//        TimeUnit.SECONDS.sleep(40);
//        simpleMessageListenerContainer.removeQueueNames("mail","weixin");
//        System.out.println("remove queue success");

//        simpleMessageListenerContainer.start();
//        System.out.println("message container startup");
//        TimeUnit.SECONDS.sleep(20);
//        simpleMessageListenerContainer.stop();
//        System.out.println("message container stop");
        TimeUnit.SECONDS.sleep(3600);
        System.out.println("------执行完成--------");
        context.close();
    }
}
